package com.jereh.cloudmusic.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.jereh.cloudmusic.PlayApplication;
import com.jereh.cloudmusic.entity.Song;
import com.jereh.cloudmusic.ible.IMusic;

import java.io.IOException;

public class PlayerService extends Service {

    private MediaPlayer mp;
    private PlayApplication application;

    @Override
    public void onCreate() {
        super.onCreate();
        application = (PlayApplication) getApplication();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        play(0);
        return new PlayerBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        mp.stop();
        mp.release();
        mp = null;
        return super.onUnbind(intent);
    }

    public void play(int position) {
        Log.e("jereh","size:"+application.songsList.size());
        if(application.songsList.isEmpty()){
            return;
        }
        if(position<0) {
            position=application.songsList.size()+position;
        }
        application.songItemPos=position;//在全局变量中标记当前播放位置
        Song currentSong = application.songsList.get(position);//获取当前播放音乐
//        Uri musicTableForSD = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//        Uri uri = Uri.withAppendedPath(musicTableForSD,
//                "/" + currentSong.getSonginfo().getAlbum_id());//初始化mp对象
        Uri uri=Uri.parse(currentSong.getBitrate().getFile_link());
        mp=new MediaPlayer();
        try {
            mp.setDataSource(this,uri);
            mp.prepareAsync();
            mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
//        mp = MediaPlayer.create(this, uri);
        Toast.makeText(PlayerService.this, "歌曲名："+currentSong.getSonginfo().getTitle(), Toast.LENGTH_SHORT).show();
//        System.out.println(currentSong.getSonginfo().getTitle());

    }
    public class PlayerBinder extends Binder implements IMusic{

        @Override
        public void moveon() {
            Toast.makeText(PlayerService.this, "moveon", Toast.LENGTH_SHORT).show();
            if (mp != null) {
                mp.start();
            }
        }

        @Override
        public void pause() {
            Toast.makeText(PlayerService.this, "pause", Toast.LENGTH_SHORT).show();
            if (mp != null) {
                mp.pause();
            }
        }

        @Override
        public void stop() {
            Toast.makeText(PlayerService.this, "stop", Toast.LENGTH_SHORT).show();
            if (mp != null) {
                mp.stop();
                mp.release();
                mp = null;
            }
        }

        @Override
        public void nextSong() {
            Toast.makeText(PlayerService.this, "nextSong", Toast.LENGTH_SHORT).show();
            if (mp != null) {
                mp.stop();
                play(++application.songItemPos);
            }
        }

        @Override
        public void lastSong() {
            Toast.makeText(PlayerService.this, "lastSong", Toast.LENGTH_SHORT).show();
            if (mp != null) {
                mp.stop();
                play(--application.songItemPos);
            }
        }
    }
}
